﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using hzGis.model;
using System.Data;
using hzGis.utils;

namespace hzGis.service.impl
{
   public class DBService: IDBService
    {
       public YdbpXm queryYdbpXmById(ToolConfig toolConfig, long id)
        {
            MySqlConnection msc = DBHelper.getConnection(toolConfig.dbUrl, toolConfig.dbUser, toolConfig.dbPwd, toolConfig.dbName, toolConfig.dbPort);
            string sql = "select * from ydbp_xm where id = " + id;
            MySqlDataAdapter mda = new MySqlDataAdapter(sql, msc);
            DataSet ds = new DataSet();
            mda.Fill(ds);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                YdbpXm ydbpXm = new YdbpXm();
                ydbpXm.bphxFile = dr["bphx_file"].ToString();
                ydbpXm.fxjgFile = dr["fxjg_file"].ToString();
                ydbpXm.hty = dr["hty"].ToString();
                ydbpXm.xmmc = dr["xmmc"].ToString();
                ydbpXm.bphxShp = dr["bphx_shp"].ToString();
                return ydbpXm;
            }
            else
            {
                return null;
            }
        }


       public double sumArea(ToolConfig toolConfig, int afterYear, long id)
       {
           MySqlConnection msc = DBHelper.getConnection(toolConfig.dbUrl, toolConfig.dbUser, toolConfig.dbPwd, toolConfig.dbName, toolConfig.dbPort);
           string sql = "select jsyd from ydbp_xm_fx_mx where nf = '"+afterYear+"' ";
           MySqlDataAdapter mda = new MySqlDataAdapter(sql, msc);
           DataSet ds = new DataSet();
           mda.Fill(ds);
           DataTable dt = ds.Tables[0];
           double area = 0;
           for (var i = 0; i < dt.Rows.Count; i++)
           {
               DataRow dr = dt.Rows[i];
               double jsyd = (double)dr["jsyd"];
               area += jsyd;
           }
           area = area / 10000;
           return Math.Round(area, 4, MidpointRounding.AwayFromZero);
       }
    }
}
